#include "pacman.h"
#include "ui_pacman.h"
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<QRandomGenerator>

pacman::pacman(QWidget *parent):
    QMainWindow(parent) ,
      ui(new Ui::pacman),blsRun(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,290,310));

}

pacman::~pacman()
{
    delete ui;
}

void pacman::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsRun){
        Init();
    }

    resize(660,660);

    painter.setPen(Qt::white);
    painter.setBrush(Qt::gray);
    painter.drawRect(30,30,600,600);

    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawRect(40,40,580,580);
    painter.drawPixmap(40,40,580,580,QPixmap(":/picture/images/bg.jpg"));

    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawRect(600,600,20,20);
    painter.drawPixmap(600,600,20,20,QPixmap(":/picture/images/exit.jpg"));

    QFont font1("Courier",60);
    painter.setFont(font1);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(30,300,sDisplay);

    QFont font2("Courier",36);
    painter.setFont(font2);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(70,550,starttext2);

    QFont font4("Courier",70);
    painter.setFont(font4);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(150,130,title);

    QFont font5("Courier",18);
    painter.setFont(font5);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(110,400,S);

    QFont font6("Courier",18);
    painter.setFont(font6);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(110,430,D);

    QFont font7("Courier",18);
    painter.setFont(font7);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(110,460,A);

    QFont font8("Courier",18);
    painter.setFont(font8);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(400,400,simple);

    QFont font9("Courier",18);
    painter.setFont(font9);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(400,430,normal);

    QFont font10("Courier",18);
    painter.setFont(font10);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(400,460,difficult);

    QFont font11("Courier",18);
    painter.setFont(font11);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(40,180,declare);

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(x1[i]);
    painter.drawPixmap(x1[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(x2[i]);
    painter.drawPixmap(x2[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(x3[i]);
    painter.drawPixmap(x3[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(x4[i]);
    painter.drawPixmap(x4[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(x5[i]);
    painter.drawPixmap(x5[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(y1[i]);
    painter.drawPixmap(y1[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(y2[i]);
    painter.drawPixmap(y2[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(y3[i]);
    painter.drawPixmap(y3[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(y4[i]);
    painter.drawPixmap(y4[i],QPixmap(":/picture/images/CT.jpg"));
    }

    for(int i=0;i<5;i++){
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::red);
    painter.drawRect(y5[i]);
    painter.drawPixmap(y5[i],QPixmap(":/picture/images/CT.jpg"));
    }

    painter.setPen(Qt::white);
    painter.setBrush(Qt::blue);
    painter.drawRect(pac);
    painter.drawPixmap(pac,QPixmap(":/picture/images/OIP-C.jpg"));
    if(blsOver)
        timer->stop();
}
void pacman::Init(){
    nDirection=2;
    blsRun=true;
    blsOver=false;
    title="ESCAPE!";
    declare="让左上角的劫匪逃脱警察的追捕丛右下角的出口逃走吧！";
    S="向下移动：S";
    D="向右移动：D";
    A="停在原地：A/W";
    simple="简单：1";
    normal="普通：2";
    difficult="困难：3";
    starttext2="按数字键加空格开始游戏";
    QRect rect(40,40,20,20);
    pac=rect;
    for(int i=0;i<5;i++){
        x1[i]=CreateRectx();
    }
    for(int i=0;i<5;i++){
        x2[i]=CreateRectx();
    }
    for(int i=0;i<5;i++){
        x3[i]=CreateRectx();
    }
    for(int i=0;i<5;i++){
        x4[i]=CreateRectx();
    }
    for(int i=0;i<5;i++){
        x5[i]=CreateRectx();
    }
    for(int i=0;i<5;i++){
        y1[i]=CreateRecty();
    }
    for(int i=0;i<5;i++){
        y2[i]=CreateRecty();
    }
    for(int i=0;i<5;i++){
        y3[i]=CreateRecty();
    }
    for(int i=0;i<5;i++){
        y4[i]=CreateRecty();
    }
    for(int i=0;i<5;i++){
        y5[i]=CreateRecty();
    }
    timer=new QTimer(this);
    timer->start(speed);
    connect(timer,SIGNAL(timeout()),SLOT(pacman_update()));

}

void pacman::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key()){
    case Qt::Key_W:nDirection=1;
        break;
    case Qt::Key_S:nDirection=2;
        break;
    case Qt::Key_A:nDirection=3;
        break;
    case Qt::Key_D:nDirection=4;
        break;
    case Qt::Key_Space:blsstart=true;
        break;
    case Qt::Key_1:speed=500;
        break;
    case Qt::Key_2:speed=100;
        break;
    case Qt::Key_3:speed=50;
        break;
    default:;
   }
}
void pacman::pacman_update(){
     if(blsstart){
    declare="";
    S="";
    D="";
    A="";
    simple="";
    normal="";
    difficult="";
    starttext2="";
    for(int i=0;i<5;i++){
        x1[i].setTop(x1[i].top()+20);
        x1[i].setBottom(x1[i].bottom()+20);
    }
    for(int i=0;i<5;i++){
        y1[i].setLeft(y1[i].left()+20);
        y1[i].setRight(y1[i].right()+20);
    }
    if(n>5){
        for(int i=0;i<5;i++){
            x2[i].setTop(x2[i].top()+20);
            x2[i].setBottom(x2[i].bottom()+20);
        }
        for(int i=0;i<5;i++){
            y2[i].setLeft(y2[i].left()+20);
            y2[i].setRight(y2[i].right()+20);
        }
    }
    if(n>20){
        for(int i=0;i<5;i++){
            x3[i].setTop(x3[i].top()+20);
            x3[i].setBottom(x3[i].bottom()+20);
        }
        for(int i=0;i<5;i++){
            y3[i].setLeft(y3[i].left()+20);
            y3[i].setRight(y3[i].right()+20);
        }
    }
    if(n>30){
        for(int i=0;i<5;i++){
            x4[i].setTop(x4[i].top()+20);
            x4[i].setBottom(x4[i].bottom()+20);
        }
        for(int i=0;i<5;i++){
            y4[i].setLeft(y4[i].left()+20);
            y4[i].setRight(y4[i].right()+20);
        }
    }
    if(n>40){
        for(int i=0;i<5;i++){
            x5[i].setTop(x5[i].top()+20);
            x5[i].setBottom(x5[i].bottom()+20);
        }
        for(int i=0;i<5;i++){
            y5[i].setLeft(y5[i].left()+20);
            y5[i].setRight(y5[i].right()+20);
        }
    }

    switch(nDirection)
    {
    case 1:
        pac.setTop(pac.top()-20);
        pac.setBottom(pac.bottom()-20);
    case 2:
        pac.setTop(pac.top()+20);
        pac.setBottom(pac.bottom()+20);
    case 3:
        pac.setLeft(pac.left()-20);
        pac.setRight(pac.right()-20);
    case 4:
        pac.setLeft(pac.left()+20);
        pac.setRight(pac.right()+20);
    default:;
    }

    if(pac.left()==600 && pac.top()==600){
        sDisplay="WIN：成功逃脱！";
         blsOver=true;
    }
    if(pac.left()<40||pac.right()>620||pac.top()<40||pac.bottom()>620){
        sDisplay="lost：无路可逃！";
        blsOver=true;
    }
    for(int i=0;i<5;i++){
        if(pac==x1[i]||pac==y1[i]||pac==x2[i]||pac==y2[i]||pac==x3[i]||pac==y3[i]||pac==x4[i]||pac==y4[i]||pac==x5[i]||pac==y5[i]){
            sDisplay="lost：落入法网!";
            blsOver=true;
        }
    }
    if(n>65){
        sDisplay="lost：超时!";
        blsOver=true;
    }
    n++;
    update();
}
     if(speed==50||speed==100||speed==500){
         timer->stop();
         timer->start(speed);
     }
}

QRect pacman::CreateRectx(){
    int x;
    do{
    x=rand()%30;}while(x==1||x==2||x==0);
    QRect rect(20+x*20,20,20,20);
    return rect;
}
QRect pacman::CreateRecty(){
    int y;
    do{
    y=rand()%30;}while(y==2||y==1||y==0);
    QRect rect(20,20+y*20,20,20);
    return rect;
}


